$(document).ready(function(){
    
    //external
    $('a[rel="external"]').attr('target', '_blank');
    
    //layout
    resizeContent();
    //scrollButton.initialPos = $('.buttons.attop').css('top');
    //scrollButton();
    $(window)
        .resize(function(){ resizeContent(); });
        //.scroll(function(){ scrollButton(); })

    //menu
    $('#menu li').each(function(){
        var hasSub = $(this).children('ul').length;
        if (hasSub > 0){
            $(this)
                .css({
                    'position'  : 'relative',
                    'z-index'   : '600'
                })
                .hoverIntent(
                    function(){ $(this).addClass('over').children('ul').fadeIn('fast'); },
                    function(){ $(this).removeClass('over').children('ul').fadeOut('fast'); }
                );
        }
    });

    //separator
    $('#separator').bind('click', function(){
        $(this).toggleClass('collapsed');
        if ($('#aside').css('display') == 'none'){
            $('#aside').show();
        }
        else $('#aside').hide();
    });

    //accordion
//    $('.accordion').accordion({
//        autoHeight  : false,
//        navigation  : true
//    });

    //datepicker
    $('.datepicker').datepicker({
        showButtonPanel: true,
        currentText: 'Hôm nay'
    });

    //message
    $('.message').each(function(){
        $(this).children('a.close').click(function(){
            $(this).parent().fadeOut();
            return false;
        });
    });

    //sortable
    $('.sortable .column').sortable({
        connectWith : '.column',
        handle      : '.header'
    }).disableSelection();

    //functions
    $('.table .functions').each(function(){
        $(this).hover(
            function(){ $(this).addClass('expanded'); },
            function(){ $(this).removeClass('expanded'); }
        );
    });

    //tabs
    $('.tabs').tabs();

    //dialog
    $('#dialog').dialog('destroy');
    $('#dialog').dialog({
        autoOpen    : false,
        height      : 250,
        modal       : true,
        width       : 400
    });
    $('#user .login').click(function(){
        $('#dialog').dialog('open');
        return false;
    });

    //search
    $('.search input[type="text"]').each(function(){
        var defaultVal = $(this).val();
        $(this).focus(function(){
            if ($(this).val() == defaultVal){
                $(this).val('');
            }
        }).blur(function(){
            if (jQuery.trim($(this).val()) == ''){
                $(this).val(defaultVal);
            }
        });
    });
    $('.search fieldset .panel .content').hide();
    $('.search fieldset .panel .trigger').bind('click', function(){
        $(this).siblings('.content').slideToggle();
    });

    //file
    //$('input.file').filestyle({
//        imageheight : 21,
//        imagewidth  : 73,
//        width       : 200
//    });
    
    // Đổi mật khẩu người dùng hiện tại
    $("#changeCurrentUserPass").dialog({
		width: 400,
		resizable : false,
		autoOpen : false
	});
	
	$("a.changeCurrentUserPassLink").click(function(){
		var id_change = $(this).attr('id_change');
		$('#changeCurrentUserPass').dialog('open');
		$('input#changeID').val(id_change);
		return false;
	});
	
	var options = { 
		dataType: 'json',
	    success: function(responseContent) { 
	        alert(responseContent.message);
	        if (!responseContent.error) {
	        	$('#changePassword')[0].reset();
	        	$("#changeCurrentUserPass").dialog('close');
	        }
	    } 
	};
	
	$('#changePassword').ajaxForm(options);
});

function resizeContent(){
    if ($(window).width() < 960) { $('#container').css('width', '960px'); }
    else $('#container').css('width', '100%');
    //$('.buttons.attop').css('width', $('#content').width());
}

function scrollButton(){
    var btnTop = $('.buttons.attop');
    if (!btnTop) return;
    var currentOffset = document.documentElement.scrollTop || document.body.scrollTop;
    var startPos = parseInt(scrollButton.initialPos) || 120;
    var desiredOffset = startPos - currentOffset;
    if (desiredOffset < 0) desiredOffset = 0;
    if (desiredOffset != parseInt($('.buttons.attop').css('top'))) $('.buttons.attop').css('top', desiredOffset);
}